home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 1 / Meeting Pearls Vol 1 (1994).iso / installed_progs / gfx / lise2.1 / lise / src / str / strtail.c < prev   
Encoding:
C/C++ Source or Header  |  1993-03-31  |  1.4 KB  |  64 lines

  1. #include <stdio.h>
  2. main(argc,argv)
  3. int argc;
  4. char *argv[];
  5. {
  6. char c,s[256],z[256],e[256];
  7. int i,n,o;
  8.  
  9. if(argc==1) { /* without parameter return instructions */
  10.    printf("strtail returns a substring from a string, starting after\n");
  11.    printf("the specified characters\n");
  12.    printf("strtail abcde.fgh de.\n");
  13.    printf("will produce \n fgh \n");
  14.    printf("you may specify an offset as optional 3rd parameter\n");
  15.    printf("\n(C) Rainer Kowallik\n");
  16. }
  17.  
  18.    o=0;
  19.    if(argc==4) o=atoi(argv[3]);
  20.  
  21.    strcpy(s,argv[1]);      /* source string */
  22.    strcpy(z,argv[2]);      /* start string */
  23.    i=instr(z,s);
  24.    strcpy(e,s);
  25.    if(i>0) midstr(e,s,i+1+o,strlen(s));
  26.    printf("%s\n",e);
  27.    exit(0);
  28. }
  29.  
  30. /* -------------------------------------------
  31.    return position of a substring in a string
  32.    ------------------------------------------- */
  33. instr(substr,str)
  34. char  str[],substr[];
  35. {  short i,p,flg,l1,l2;
  36.  
  37.    l1=strlen(str); l2=strlen(substr);
  38.    for(p=0; p < l1; p++) {
  39.       flg=0;
  40.       for(i=0; i < l2; i++) {
  41.          if(str[p+i] != substr[i]) {
  42.             flg = -1; break;
  43.          }
  44.       }
  45.       if(flg == 0) return(p);
  46.    }
  47.    return(-1);
  48. }
  49.  
  50.  
  51. /* -------------------------------------------------
  52.    return a substring from within a mainstring
  53.    ------------------------------------------------- */
  54. midstr(substr,str,ss,es)
  55. char  substr[],str[];
  56. short ss,es;
  57. {  short i,j;
  58.  
  59.    i=0;
  60.    for(j=ss; j <= es; j++) substr[i++]=str[j];
  61.    substr[i]=0;
  62. }
  63.  
  64.